package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    /**
     * 5、查询用户资料------根据id查询用户资料
     * 参数：
     * token，userID
     * 请求连接
     * Get   /users
     * 返回值：
     * UserInfo
     */
    @GetMapping
    public ResponseEntity users(Long userID) {

        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        //根据id查询userinfo
        UserInfoVo userInfoVo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 6、更新用户资料
     *
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 7、 通用——查询通用设置
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo vo = userService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 8、通用——陌生人问题——保存与修改
     * 请求地址：
     * POST  /users/questions
     * 参数：
     * Body: content
     * 返回值：
     * ResponseEntity<null>
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        String content = (String) map.get("content");
        userService.saveQuestions(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 9、通用——通知设置——状态开启与关闭
     * 请求地址：
     * POST  users/notifications/setting
     * 参数：
     * Body: likeNotification   pinglunNotification   gonggaoNotification
     * 返回值：
     * ResponseEntity<null>
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map) {
        Boolean likeNotification = (Boolean) map.get("likeNotification");
        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
        userService.saveNotifications(likeNotification, pinglunNotification, gonggaoNotification);
        return ResponseEntity.ok(null);
    }

    /**
     * 10、通用——黑名单——分页查询黑名单
     * 请求地址：
     * GET  users/blacklist?page=1&pagesize=10
     * 参数：
     * Query: page,paesize
     * 返回值：
     * ResponseEntity<PageResult>
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize
    ) {
        PageResult result = userService.blacklist(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 11、通用——黑名单——取消黑名单
     * 请求地址：
     * DELETE users/blacklist/:uid
     *
     * @param blackUserId
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) {
        userService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }
}
